/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package javax.enterprise.context.spi;

import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import java.lang.annotation.Annotation;

/**
 * <p>
 *     提供获取具有任何上下文类型的特定范围的上下文实例的操作. 任何{@code Context}实例都被称作为上下文对象
 * </p>
 *
 * <p>
 *     上下文对象负责通过 {@link Contextual} 接口中的操作来创建或销毁上下文实例;
 * 特别是, 上下文对象负责通过将实例传递给 {@link Contextual#destroy(Object, CreationalContext)} 来销毁它创建的任何上下文实例。被销毁的实例不能被{@code get()}方法返回.
 *
 *     上下文对象必须将相同的CreationalContext 实例传递给  {@code Contextual.destroy()},
 * 当然在实例创建的时候会调用{@code Contextual.create()}，所以也要传递相同的CreationalContext 实例给{@code Contextual.create()}方法
 * </p>
 *
 * <p>
 *     自定义上下文对象可以使用{@link AfterBeanDiscovery#addContext(Context)}向容器注册 (ps:指运行阶段动态的添加上下文对象)
 * </p>
 *
 * @author Gavin King
 * @author Pete Muir
 */

public interface Context {

    /**
     * 获取上下文对象的Scope Type
     *
     * @return the scope
     */
    public Class<? extends Annotation> getScope();

    /**
     * 返回特定上下文类型的现有实例，如果不存在，则通过调用{@link Contextual#create(CreationalContext)} 来创建并返回一个新的实例
     *
     * @param <T>  contextual 类型
     * @param contextual   contextual 类型
     * @param creationalContext 将要被创建新实例的creationalContext
     * @return contextual实例
     * 
     * @throws ContextNotActiveException  如果该上下文对象没有被激活(isActive() == false) , 则抛出该异常
     */
    public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext);

    /**
     * 返回特定上下文类型的现有实例，如果不存在，则返回空
     * 
     * @param <T> contextual 类型
     * @param contextual contextual 类型
     * @return contextual实例 / null
     * 
     * @throws ContextNotActiveException 如果该上下文对象没有被激活(isActive() == false), 则抛出该异常
     */
    public <T> T get(Contextual<T> contextual);

    /**
     * 判断该上下文对象是否被激活
     * 
     * @return true/false
     */
    public boolean isActive();

}
